package com.web3.management.controller;

import com.web3.management.dto.ApiResponse;
import com.web3.management.dto.PageResponse;
import com.web3.management.dto.request.AccountRequest;
import com.web3.management.dto.response.AccountClientTokenResponse;
import com.web3.management.dto.response.AccountDetailResponse;
import com.web3.management.dto.response.AccountListResponse;
import com.web3.management.dto.response.EffectiveConfigResponse;
import com.web3.management.service.AccountService;
import com.web3.management.service.AccountConfigService;
import com.web3.management.security.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 账号管理控制器
 */
@RestController
@RequestMapping("/api/accounts")
@RequiredArgsConstructor
@Validated
public class AccountController {

    private final AccountService accountService;
    private final AccountConfigService accountConfigService;

    @GetMapping
    public ApiResponse<PageResponse<AccountListResponse>> listAccounts(
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "10") @Min(1) Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status) {
        PageResponse<AccountListResponse> response = accountService.getAccountPage(page, size, name, status);
        return ApiResponse.success(response);
    }

    @GetMapping("/{id}")
    public ApiResponse<AccountDetailResponse> getAccount(@PathVariable @NotNull @Min(1) Integer id) {
        return ApiResponse.success(accountService.getAccountDetail(id));
    }

    @GetMapping("/{id}/effective-config")
    public ApiResponse<EffectiveConfigResponse> getEffectiveConfig(@PathVariable @NotNull @Min(1) Integer id) {
        SecurityUtils.checkAccountAccess(id);
        EffectiveConfigResponse response = accountConfigService.getEffectiveConfig(id);
        return ApiResponse.success(response);
    }

    @PostMapping("/{id}/client-token")
    public ApiResponse<AccountClientTokenResponse> generateClientToken(@PathVariable @NotNull @Min(1) Integer id) {
        AccountClientTokenResponse response = accountService.generateClientToken(id);
        return ApiResponse.success("客户端Token生成成功", response);
    }

    @PostMapping
    public ApiResponse<AccountDetailResponse> createAccount(@Valid @RequestBody AccountRequest request) {
        AccountDetailResponse response = accountService.createAccount(request);
        return ApiResponse.success("账号创建成功", response);
    }

    @PutMapping("/{id}")
    public ApiResponse<AccountDetailResponse> updateAccount(@PathVariable @NotNull @Min(1) Integer id,
                                                             @Valid @RequestBody AccountRequest request) {
        AccountDetailResponse response = accountService.updateAccount(id, request);
        return ApiResponse.success("账号更新成功", response);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteAccount(@PathVariable @NotNull @Min(1) Integer id) {
        accountService.deleteAccount(id);
        return ApiResponse.success("账号删除成功", null);
    }

    /**
     * 重启客户端 - 标记账号需要重启
     */
    @PostMapping("/{id}/restart")
    public ApiResponse<Void> restartClient(@PathVariable @NotNull @Min(1) Integer id) {
        SecurityUtils.requireAdmin();
        accountService.markNeedsRestart(id);
        return ApiResponse.success("重启指令已发送", null);
    }
}
